package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.EmployeeListAdapter;
import com.novellotus.iov.carvice.bean.RxEusers;
import com.novellotus.iov.carvice.bean.RxFn;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.rxjava.MyObserver;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class TeamManagementActivity extends BaseActivity implements RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface, EmployeeListAdapter.ClickEmployee {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.btn_right)
    Button btn_right;
    @BindView(R.id.pageView)
    PageView pageView;
    @BindView(R.id.edt_name)
    EditText edt_name;
    EmployeeListAdapter employeeListAdapter;
    private final int PAGE_SIZE = 10;
    private String eCode;
    private String rCode;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_team_management);
    }

    @Override
    public void init() {
        eCode = getIntent().getStringExtra("eCode");
        rCode = getIntent().getStringExtra("rCode");
        tv_tittle.setText("团队管理");
        btn_right.setText("邀请员工");
        btn_right.setTextColor(getResources().getColor(R.color.white));
        pageView.refreshLayout.setOnRefreshListener(this);
        employeeListAdapter = new EmployeeListAdapter(null, this);
        pageView.setPageViewInterface(this);
        pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(this), employeeListAdapter);
        pageView.setReLoadBtnListener(this);
        getRoleList();
        setSearchEdit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        int sizeNow = pageView.getLoadCurrentPage() * 10;
        getList(1, sizeNow);
    }

    /**
     * 设置搜索框
     */
    private void setSearchEdit() {
        RxTextView.textChanges(edt_name).subscribe(new MyObserver<CharSequence>() {
            @Override
            public void onNext(CharSequence value) {
                super.onNext(value);
                MyUtils.logInformation("搜索");
                getList(pageView.getLoadCurrentPage(), PAGE_SIZE);
            }
        });
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_right)
    void right() {
        Bundle bundle = new Bundle();
        bundle.putString("e", eCode);
        bundle.putString("r", rCode);
        bundle.putParcelableArrayList("roleList", (ArrayList) rxFnList);
        openActivity(AskEmployeeActivity.class, bundle);
    }

    @Override
    public void loadData(int dataPage) {
        getList(pageView.getLoadCurrentPage(), PAGE_SIZE);
    }

    @Override
    public void onReload() {
        getRoleList();
        getList(1, PAGE_SIZE);
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }


    /**
     * 获取员工列表
     */
    public void getList(int current, int size) {
        Observable<BaseEntity<RxEusers>> observable = RetrofitFactory.getInstance().eusers(SharedPreferencesUtils.getKey(this)[0], eCode, rCode, current, size, edt_name.getText().toString());
        observable.compose(RxSchedulers.<BaseEntity<RxEusers>>lifecycle(this))
                .compose(RxSchedulers.<BaseEntity<RxEusers>>compose())
                .subscribe(new BaseObserver<RxEusers>(this) {
                    @Override
                    public void onSuccess(RxEusers rxEusers) {
                        pageView.loadSuccess();
                        if (rxEusers != null) {
                            if (rxEusers.getRecords() == null || rxEusers.getRecords().size() == 0) {
                                pageView.loadFail("无相关数据", false);
                            }
                            pageView.setTotalPage(rxEusers.getTotal(), PAGE_SIZE);
                            if (rxEusers.getCurrent() == 1) {
                                employeeListAdapter.update(rxEusers.getRecords());

                            } else {
                                employeeListAdapter.addData(rxEusers.getRecords());
                            }
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.loadFail(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.loadFail(msg, true);
                    }

                });
    }

    /**
     * 获取角色列表
     */
    List<RxFn> rxFnList = new ArrayList<>();

    public void getRoleList() {
        Observable<BaseArrayEntity<RxFn>> observable = RetrofitFactory.getInstance().fn(SharedPreferencesUtils.getKey(this)[0], eCode, rCode);
        observable.compose(RxSchedulers.<BaseArrayEntity<RxFn>>compose())
                .subscribe(new BaseArrayObserver<RxFn>(this) {
                    @Override
                    public void onSuccess(List<RxFn> list) {
                        rxFnList = list;
                    }

                    @Override
                    public void onFail(int code, String msg) {

                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
    }

    @Override
    public void clickItem(RxEusers.RecordsBean recordsBean) {
        Bundle bundle = new Bundle();
        bundle.putString("e", eCode);
        bundle.putString("r", rCode);
        bundle.putString("id", recordsBean.getId());
        bundle.putString("userId", recordsBean.getUserId());
        bundle.putString("name", recordsBean.getEUserName());
        bundle.putString("roleId", recordsBean.getRoleId());
        bundle.putString("mark", recordsBean.getRemark());
        bundle.putParcelableArrayList("roleList", (ArrayList) rxFnList);
        openActivity(EditEmployeeActivity.class, bundle);
    }
}
